package com.nuoniu.sibanyun.controller.finance;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.finance.VVoucherTemplate;
import com.nuoniu.sibanyun.service.finance.IVVoucherTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.tools.ant.taskdefs.Javadoc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 凭证模板表 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2021-12-18
 */
@RestController
@RequestMapping("/voucher/template")
@Api(tags = "凭证模板管理")
public class VVoucherTemplateController extends BaseController {

    @Autowired
    private IVVoucherTemplateService voucherTemplateService;

    @ApiOperation("获取列表")
    @GetMapping("getList")
    public HttpResult getList(Page page, String voucherType) {
        return HttpResult.ok(voucherTemplateService.getList(page, voucherType));
    }

    @ApiOperation("获取详情")
    @GetMapping("getDetail")
    public HttpResult getDetail(Long id) {
        return HttpResult.ok(voucherTemplateService.getDetail(id));
    }

    @ApiOperation("保存")
    @PostMapping("save")
    public HttpResult save(@RequestBody VVoucherTemplate template) {
        if(template == null || StringUtils.isBlank(template.getWord())) {
            throw new NuoNiuException("凭证字不能为空");
        }

        if(template.getVoucherTemplateDetailList().size() == 0) {
            throw new NuoNiuException("明细不能为空");
        }

        voucherTemplateService.saveTemplate(template);
        return HttpResult.ok();
    }

    @ApiOperation("删除")
    @DeleteMapping("delete")
    public HttpResult delete(Long id) {
        voucherTemplateService.delete(id);
        return HttpResult.ok();
    }
}
